package org.mifos.boss.ui.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.mifos.boss.domainmodel.Customer;
import org.mifos.boss.domainmodel.MobileFileUpload;
import org.mifos.boss.service.CustomerServiceInterface;
import org.mifos.boss.service.DataTransferServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.client.SoapFaultClientException;


@Controller
@RequestMapping(value = "/customer")
public class CustomerController extends AbstractController {

	static Logger logger = Logger.getLogger(CustomerController.class);

	@Autowired
	@Qualifier("customerService")
	private CustomerServiceInterface customerService;
	
	

	@Resource(name="webServiceTemplate")
	 private WebServiceTemplate webServiceTemplate;
	
    /**
     * Adds a new customer by delegating the processing to CustomerServiceImpl.
     * Displays a confirmation JSP page
     * 
     * @return  the name of the JSP page
     */
   @RequestMapping(value = "/newcustomers/add")
    public String initiateNewCustomerAddition(@ModelAttribute("customerData") Customer customer, BindingResult result) {
		
    	logger.debug("Received request to add new customer");
        
        // The "subscriptionAttribute" model has been passed to the controller from the JSP
        // We use the name "subscriptionAttribute" because the JSP uses that name
         
        // Delegate to webServiceTemplate for the actual subscription
        try {
            webServiceTemplate.marshalSendAndReceive(customer);
            logger.error("Web service consumed");
             
            // Add response to model so that we know if successful or not
          
        } catch (SoapFaultClientException sfce) {
          
         // This indicates there's something wrong with our message
         // For example a validation error
         logger.error("We sent an invalid message", sfce);
          
          
        } catch (Exception e) {
         // Expect the unexpected
         logger.error("Unexpected exception", e);
          
        }
         
         
        // Show the subscribe page again regardless if successful or not
        // This will resolve to /WEB-INF/jsp/subscribepage.jsp
  
    	
    	
    	
    	
    	// The "customerData" model has been passed to the controller from the JSP
    	// We use the name "customerData" because the JSP uses that name
    			
		// Call customerService to do the actual adding
//		customerService.createCustomer(customer);
//		customerService.
		
    	// This will resolve to /WEB-INF/jsp/addedpage.jsp
		return "addcustomersuccesspage";
	}

//    /**
//     * Dummy method to display the home page, where user can enter Customer Information
//     */
//	@RequestMapping("/login")
//	public String displayHome(Model model) {
//		logger.debug("Show the login page");
//    	
//    	model.addAttribute("customerData", new Customer());
//    	
//    	return "index";
////    	return "index";
//    	
//	}

    /**
     * Method to display page to upload data from MMFMSMobile to BOSS.
     * 
     */
	@RequestMapping("/upload")
	public String uploadData(Model model) {
		logger.debug("Upload data from MFMS Mobile to BOSS");

		model.addAttribute("mobileFileUpload", new MobileFileUpload());
    	return "dataupload";
	}
	

    	
}
